package wah;

import static robocode.util.Utils.normalRelativeAngleDegrees;
import robocode.Robot;
import robocode.HitByBulletEvent;
import robocode.ScannedRobotEvent;

/**
 * @author Wahib Hanani 
 */
public class WahibRobot extends Robot {
  
  int movementDirection = 1;
  int hitCounter = 0;
 // String trackName;
  double gunTurnAmount;
  //double previousEnergy = 100;
  //double changeInEnergy;
  double height;
 // boolean rightEdge = true;

  /**
   * Creates a robot that, walks (back and forth)on the right wall of the battle field and tracking
   * an enemy by its gun with firing bullets with lowest power.
   */
  public void run() {
  //  trackName = null;
    gunTurnAmount = 360;
    height = getBattleFieldHeight();
    turnRight(270 - getHeading());
    back(getBattleFieldWidth() - this.getX());

    turnRight(90);
    turnGunRight(-90);
    ahead((height) * movementDirection);
    movementDirection = -movementDirection;
     
    while (true) {
      turnGunRight(gunTurnAmount);
      ahead((height) * movementDirection);
      movementDirection = -movementDirection;
    }
  }
  
  /**
   * Makes the robot turn towards the enemy and point its gun toward the chosen enemy then
   * fires bullets at that enemy with power of 1 if the distance bigger than 150 and 3 if else.
   * @param e contains information about the enemy robot, e.g. its location
   */
  public void onScannedRobot(ScannedRobotEvent e) {
 
    //Gets the angle to point the gun to a chosen enemy
    gunTurnAmount = normalRelativeAngleDegrees(e.getBearing() + (getHeading() - getRadarHeading()));
    turnGunRight(gunTurnAmount);//Sets the gun pointing to chosen enemy
 
    if (e.getDistance() < 150 || e.getBearing() == this.getHeading() || hitCounter > 2) {
      fire(3);
      hitCounter = 0;
      scan();
    }
    fire(1);
    scan();
  }
  
  /**
   * Increase hit counters when the robot hit by bullets.
   * @param e contains information about the enemy robot, e.g. its location
   */
  public void onHitByBullet(HitByBulletEvent e) {
    hitCounter++;
  }

}
